package com.syqc.choose.teamcar

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.autoDispose
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.comlib.base.LazyFragment
import com.syqc.comlib.utils.loadGif
import com.syqc.comlib.utils.setResultTo
import com.syqc.comlib.utils.toast
import com.syqc.entity.TeamsAndCars
import com.syqc.monitor.R
import com.syqc.monitor.databinding.FragmentChooseCarBinding
import com.syqc.monitor.databinding.IncludeChooseTeamCarBinding
import com.syqc.utils.Keys
import com.syqc.utils.ToolsUtil
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers

/**
 *@author joker
 *@time   2024/6/1 8:37
 *@desc
 */
class TeamsFragment :
    LazyFragment<FragmentChooseCarBinding>(FragmentChooseCarBinding::inflate) {
    private var isMap: Boolean = false
    private var isChoose: Boolean = false
    private val mAdapter by lazy { TeamsAdapter() }
    private val mViewModel by activityViewModels<TeamsVM>()
    private lateinit var mChooseTeamVB: IncludeChooseTeamCarBinding

    companion object {
        fun instance(isMap: Boolean, isChose: Boolean): TeamsFragment {
            val fragment = TeamsFragment()
            fragment.isMap = isMap
            fragment.isChoose = isChose
            return fragment
        }
    }

    //空布局
    private val emptyView by lazy {
        val view = layoutInflater.inflate(
            R.layout.layout_empty_view,
            binding.includeRecycler.recyclerView, false
        )
        view.findViewById<ImageView>(R.id.ivNoData).loadGif(requireContext(), R.mipmap.ic_empty)
        view
    }

    override fun initView(view: View) {
        mChooseTeamVB = IncludeChooseTeamCarBinding.bind(binding.root)
        mViewModel.getTeamsCars()
        mViewModel.toastObserver().observeOn(AndroidSchedulers.mainThread())
            .autoDispose(scopeProvider).subscribe(::toastObserver)

        binding.includeRecycler.recyclerView.layoutManager = LinearLayoutManager(requireContext())
        binding.includeRecycler.recyclerView.adapter = mAdapter
        mAdapter.isChoose = isChoose
    }

    override fun initData() {
        mViewModel.nameLive.observe(this) {
            mChooseTeamVB.tvTeamName.text = it
        }

        mViewModel.teamsLive.observe(this) {
            if (it.isNullOrEmpty()) {
                mAdapter.setEmptyView(emptyView)
            } else {
                mAdapter.setNewInstance(it)
            }
        }
    }

    override fun initEvent() {
        mChooseTeamVB.tvTeamBack.setOnClickListener {
            mViewModel.getPrevious()
        }
        mChooseTeamVB.tvTeamHome.setOnClickListener {
            mViewModel.getRootTeam()
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val teamEntity = mAdapter.data[position]
            mViewModel.getTeamCar(teamEntity.teamId!!, teamEntity.teamName!!)
        }

        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (ToolsUtil.isFastClick()) return@setOnItemChildClickListener
            val teamInfo = mAdapter.data[position]
            if (isChoose && teamInfo.noAuth) {
                R.string.toast_team_noAuth.toast(requireContext())
                return@setOnItemChildClickListener
            }
            if (!isChoose && teamInfo.carNum <= 0) {
                R.string.toast_choose_no_car.toast(requireContext())
                return@setOnItemChildClickListener
            }
            if (isMap && teamInfo.carNum > 500) {
                R.string.toast_team_cars_much.toast(requireContext())
                return@setOnItemChildClickListener
            }
            setResultTo(requireActivity()) {
                putExtra(Keys.INTENT_TEAM_ID, teamInfo.teamId)
                putExtra(Keys.INTENT_TEAM_NUM, teamInfo.carNum.toString())
                putExtra(Keys.INTENT_TEAM_NAME, teamInfo.teamName)
                putExtra(Keys.INTENT_NO_AUTH, teamInfo.noAuth.toString())
            }
            mViewModel.saveData()
            requireActivity().finish()
        }
    }

}

class TeamsAdapter :
    BaseQuickAdapter<TeamsAndCars, BaseViewHolder>(R.layout.item_choose_car) {
    //当数量为0是否显示
    var isChoose = false

    init {
        addChildClickViewIds(R.id.tvItemChoose)
    }

    override fun convert(holder: BaseViewHolder, item: TeamsAndCars) {
        holder.let {
            val tvName=it.getView<TextView>(R.id.tvItemName)
            tvName.text="${item.teamName} (${item.carNum})"

            it.setImageResource(R.id.ivItemPic, R.mipmap.ic_team_pic)
            val tvItemChoose = it.getView<TextView>(R.id.tvItemChoose)
            tvItemChoose.isVisible = true
            if (isChoose) {
                tvItemChoose.isSelected = !item.noAuth
            } else {
                tvItemChoose.isSelected = item.carNum > 0
            }
            tvName.isSelected = item.noAuth
        }
    }

}